<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Classes as ClassesModel;

/**
 * Cate 课程
 * Class Cate
 * @package app\home\controller
 */
class Classes extends Base
{
    /**
     * [getIndex  获取课程]
     * @param $of 1早课 2晚课
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new ClassesModel;
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['c.of'] = $param['of'];
            $map['c.user_id'] = $mem['id'];
            $map['c.status'] = 0;
            $map['n.states'] = 1;
            $count = $model->getAllCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getLists($map,$this->from, $this->size,'c.id,c.itemid,n.title,n.cover');
            foreach($lists as $k=>$v){
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  获取课程]
     * @param $of 1早课 2晚课
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $model = new ClassesModel;
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['c.of'] = $param['of'];
            $map['c.user_id'] = $mem['id'];
            $map['c.status'] = 0;
            $map['c.create_time'] = ['gt',1597112015];
            $map['n.status'] = ['neq',2];
            $count = $model->getnewAllCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getnewLists($map,$this->from, $this->size,'c.id,c.itemid,n.title,n.cover');
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getAdd  添加课程]
     * @param $type 2佛经 3佛片 4佛乐 5禅语
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $model = new ClassesModel;
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        if(empty($param['id'])){
            return admin_json(-1, '请选择课程!');
        }
        $map = [];
        $map1 = [];
        $map2 = [];
        $data = [];
        $map1['itemid'] = ['in',$param['id']];
        $map1['user_id'] = $mem['id'];
        $map1['status'] = 0;
        $map1['of'] = $param['of'];
        $map1['create_time'] = ['gt',1597112015];
        $res = $model->getFindOne($map1,'id');
        if(!empty($res)){
            return admin_json(-1, '请去除重复课程!');
        }

        $id_arr = explode(',',$param['id']);
        try{
            $ids = '';
            foreach($id_arr as $k=>$v){
                $map['of'] = $param['of'];
                $map['user_id'] = $mem['id'];
                $map['itemid'] = $v;
                $result = $model->getFindOne($map,'id');

                if(empty($result)){
                    $data[$k]['of'] = $param['of'];
                    $data[$k]['user_id'] = $mem['id'];
                    $data[$k]['itemid'] = $v;
                }else{
                    $ids .= $v.',';
                }
            }
            $map2['itemid'] = ['in',$ids];
            $map2['user_id'] = $mem['id'];
            $model->editStatus($map2);
            $model->addDataAll($data);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [getDel  移除课程]
     * @param id：课程id
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $model = new ClassesModel;
        $param = input('param.');
        $user = $this->user;
        $map = [];
        try{
            $map['id'] = ['in',$param['ids']];
            $map['user_id'] = $user['id'];
//            $map['status'] = 1;
            $model->changeStatus($map,1);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        return admin_json(1, '删除成功');
    }
}